//
//  CMDBAddAlumCollectionView.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/20.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let KAddCollectionViewId = "KAddCollectionViewId"

class CMDBAddAlbumCollectionView: BaseCollectionView {

    var images : [UIImage] = [UIImage](){

        didSet{
   
            reloadData()
        }
    }
    
    override func commitInitOwnProperties() {
        super.commitInitOwnProperties()
        
        backgroundColor                = UIColor.whiteColor()
        
        let layout                     = collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.minimumLineSpacing      = KleftMargin
        
        layout.minimumInteritemSpacing = KleftMargin
        
        registerClass(CMDBSelectedImageCell.self, forCellWithReuseIdentifier: KAddCollectionViewId)
    }
}

extension CMDBAddAlbumCollectionView{
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSize(width: 300 * CMDB_Rate, height: 300 * CMDB_Rate + 90 * CMDB_Rate)
        
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(15, 25, 18, 25)
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        printLog("collection click")
    }
    
}

extension CMDBAddAlbumCollectionView{
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        printLog(images.count + 1)
        
        return (images.count + 1)
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(KAddCollectionViewId, forIndexPath: indexPath) as! CMDBSelectedImageCell
        
        cell.mDelegate = self
        
        if images.count < 9 {
            
            if indexPath.item == 0 {
                cell.image = nil
            }else{
                
                cell.image = images[indexPath.item - 1]
                
            }
        }else{
            
            cell.image = images[indexPath.item - 1]
            
        }
        
        

        return cell
    }
}


extension CMDBAddAlbumCollectionView{
    
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        mDelegate?.onItemClickType!("AddPhotoClick", data: nil, indexPath: nil)
    }
    
}
